package com.cmas.platform.controller;

import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.aidex.common.annotation.DataSource;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.AjaxResult;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.core.page.TableDataInfo;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.enums.DataSourceType;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.common.core.controller.BaseController;
import com.aidex.framework.cache.DictUtils;

import com.aidex.system.service.ISysUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cmas.platform.domain.Message;
import com.cmas.platform.dto.MessageDto;
import com.cmas.platform.vo.MessageVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.cmas.platform.service.MessageService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 消息表Controller
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-04-28
 */
@DataSource(value = DataSourceType.SLAVE)
@RestController
@RequestMapping("/platform/message")
public class MessageController extends BaseController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询消息表列表
     */
    @PreAuthorize("@ss.hasPermi('platform:message:list')")
    @GetMapping("/list")
    public R<TableDataInfo> list(Message message) {
        startPage();
        List<MessageVo> list = messageService.selectMessageVoList(message);
        return R.data(getDataTable(list));
    }

    /**
     * 查询最后消息表列表
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:message:list,platform:region:list')")
    @GetMapping("/lastMessageList")
    public R<TableDataInfo> lastMessageList(Message message) {
        List<Message> list = messageService.selectLastMessageList(message);
        return R.data(getDataTable(list));
    }


    /**
     * 查询用户消息表列表
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:message:list,platform:region:list')")
    @GetMapping("/userMessageList")
    public R<TableDataInfo> userMessageList(SysUser user) {
        startPage();
        List<SysUser> userList = userService.findList(user);
        List<MessageDto> list = messageService.selectuserMessageList(userList);
        TableDataInfo dataTable = getDataTable(userList);
        dataTable.setRows(list);
        return R.data(dataTable);
    }

    /**
     * 获取消息表详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:message:query')")
    @GetMapping(value = "/{id}")
    public R<Message> detail(@PathVariable("id") String id) {
        return R.data(messageService.getById(id));
    }

    /**
     * 新增消息表
     */
    @PreAuthorize("@ss.hasPermi('platform:message:add')")
    @Log(title = "消息表", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Message message) {
        return R.data(messageService.save(message));
    }

    /**
     * 修改消息表
     */
    @PreAuthorize("@ss.hasPermi('platform:message:edit')")
    @Log(title = "消息表", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Message message) {
        return R.data(messageService.updateById(message));
    }

    /**
     * 删除消息表
     */
    @PreAuthorize("@ss.hasPermi('platform:message:remove')")
    @Log(title = "消息表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.data(messageService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 导出消息表列表
     */
    @PreAuthorize("@ss.hasPermi('platform:message:export')")
    @Log(title = "消息表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Message message) {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        return util.exportExcel(list, "消息表数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    // ***用户系统位置戳****

    /**
     * 新增消息表
     */
    @Log(title = "消息表", businessType = BusinessType.INSERT)
    @PostMapping("/send")
    public R send(@RequestBody @Validated Message message) {
        return R.data(messageService.sendMessageByToId(message));
    }


    /**
     * 获得用户最后一条消息列表
     *
     * @param message
     * @return
     */
    @GetMapping("/listLastMessage")
    public R<?> listLastMessage(Message message,
                                @RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return R.data(messageService.selectLastMessageVoList(pageNo, pageSize, message));
    }


    /**
     * 获得用户聊天记录
     *
     * @param message
     * @return
     */
    @GetMapping("/userMessageRecord")
    public R<?> userMessageRecord(Message message,
                                  @RequestParam(name = "pageNum", defaultValue = "0") Integer pageNo,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        if (pageNo == 0) {
            return R.data(messageService.userMessageRecord(pageNo, pageSize, message));
        } else {
            return R.data(getDataTable(messageService.userMessageRecord(message)));
        }
    }

    /**
     *  查询聊天用户信息
     * @param userId
     * @return
     */
    @GetMapping("/messageUserInfo")
    public R<?> messageUserInfo(@RequestParam(name = "userId") String userId){
        if (StringUtils.isNotNull(userId)) {
            return R.data( userService.get(userId));
        }
            return R.fail("用户ID为空");
    }

}
